package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    //根据商品分类id查询规格组集合
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> SpenGroupById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(specGroupService.query().eq("category_id", id).list()));
    }

    //根据分类id查询规格组及其组内参数
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> SpenGroupByIds(@RequestParam("id") Long id) {
        List<SpecGroupDTO> specGroups = specGroupService.SpenGroupByIds(id);
        return ResponseEntity.ok(specGroups);
    }

    //根据条件查询规格参数集合
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> SpecParamByIds(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ){
        return ResponseEntity.ok(specParamService.SpecParamByIds(categoryId, groupId, searching));
    }

    //规格组新增
    @PostMapping("/group")
    public ResponseEntity<Void>add(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.add(specGroupDTO);
    return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //规格组修改
    @PutMapping("/group")
    public ResponseEntity<Void>updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        specGroupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //规格参数添加
    @PostMapping("/param")
    public ResponseEntity<Void>addSpecPram(@RequestBody SpecParamDTO specParamDTO){
        specParamService.addSpecPram(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //规格参数修改
    @PutMapping("/param")
    public ResponseEntity<Void>updateSpecPram(@RequestBody SpecParamDTO specParamDTO){
        specParamService.updateSpecPram(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }



}
